package com.chen.spring.cloud.miniapp.api.feign.controller;

import com.chen.entity.User;
import com.chen.spring.cloud.miniapp.api.feign.service.GoodService;
import com.chen.spring.cloud.miniapp.api.feign.service.UserService;
import com.chen.util.MapperUtils;
import com.chen.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/Good")
public class GoodController {

    @Autowired
    private GoodService goodService;

    @Autowired
    private UserService userService;

    @RequestMapping("list")
    public R list(@RequestParam(value = "gt_id") String gt_id,
                  @RequestParam(value = "hot_tab") Integer hot_tab,
                  @RequestParam(value = "limit") String limit,
                  @RequestParam(value = "is_recommend") String is_recommend,
                  @RequestParam(value = "is_hot") String is_hot){
        String goodjson = goodService.list(gt_id,hot_tab,limit,is_recommend,is_hot);
        if (goodjson != null){
            try {
                Map<String,Object> map = MapperUtils.json2map(goodjson);
                return R.ok(map);
            } catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }

    @RequestMapping("details")
    public R details(@RequestParam(value = "goods_id") String goods_id,
                  @RequestParam(value = "gt_id") Integer gt_id,
                  @RequestParam(value = "wx_openid") String wx_openid){

        if (StringUtils.isEmpty(goods_id)){
            return R.error(500,"参数不全");
        }
        int u_id = 0;
        if (!StringUtils.isEmpty(wx_openid)){
            String userjson = userService.getUidByOpenid(wx_openid);
            try {
                u_id = MapperUtils.json2pojo(userjson, User.class).getU_id();
            } catch (Exception e){
                e.printStackTrace();
            }
        }

        return null;
    }

}
